package com.vitas.coin.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.google.gson.Gson;
import com.vitas.coin.data.bean.ChildrenVoiceBean;

import java.util.ArrayList;
import java.util.List;

public class ListDao extends MyBaseDao {

    String tableName;

    public ListDao(String name) {
        tableName = name;
    }

    @Override
    protected String getTableName() {
        return tableName;
    }

    public boolean delete(ChildrenVoiceBean bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            int result = db.delete(getTableName(), "uid=?", new String[]{String.valueOf(bean.getId())});
            return result == 1;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return false;
    }

    public void delete(ChildrenVoiceBean bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                boolean bo = delete(bean);
                if (listener != null) {
                    listener.onResult(bo, "", bean);
                }
            }
        });
    }

    public ChildrenVoiceBean insert(ChildrenVoiceBean bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return null;
            }
            ContentValues values = new ContentValues();
            values.put("uid", bean.getId());
            values.put("info", new Gson().toJson(bean));
            values.put("time", System.currentTimeMillis());
            db.insert(tableName, null, values);
            return bean;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return null;
    }

    public void insert(final ChildrenVoiceBean bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                ChildrenVoiceBean tmp = insert(bean);
                if (tmp == null) {
                    if (listener != null) {
                        listener.onResult(false, "", bean);
                    }
                } else if (listener != null) {
                    listener.onResult(true, "", tmp);
                }
            }
        });
    }

    public void update(final ChildrenVoiceBean bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                SQLiteDatabase db = null;
                try {
                    db = getDataBase();
                    if (db == null) {
                        if (listener != null) {
                            listener.onResult(false, "", null);
                        }
                        return;
                    }
                    ContentValues values = new ContentValues();
                    values.put("uid", bean.getId());
                    values.put("info", new Gson().toJson(bean));
                    db.update(tableName, values, "uid=?", new String[]{bean.getId() + ""});
                    if (listener != null) {
                        listener.onResult(true, null, bean);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    if (listener != null) {
                        listener.onResult(false, e.getMessage(), null);
                    }
                } finally {
                    if (db != null) {
                        closeDataBase();
                    }
                }
            }
        });
    }

    public ArrayList<ChildrenVoiceBean> getAll() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return new ArrayList<>();
            }
            cursor = db.rawQuery("select * from " + tableName + " order by time desc", null);
            if (cursor == null || cursor.getCount() <= 0) {
                return new ArrayList<>();
            }
            ArrayList<ChildrenVoiceBean> list = new ArrayList<>();
            while (cursor.moveToNext()) {
                String str = cursor.getString(cursor.getColumnIndexOrThrow("info"));
                ChildrenVoiceBean bean = new Gson().fromJson(str, ChildrenVoiceBean.class);
                list.add(bean);
            }
            return list;
        } catch (Throwable e) {
            return new ArrayList<>();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public void getAll(DBListener listener) {
        doInBackground(new Runnable() {
            @Override
            public void run() {
                List<ChildrenVoiceBean> list = getAll();
                if (listener != null) {
                    listener.onResult(true, null, list);
                }
            }
        });
    }
}
